package com.blgroup.oa.domain.dao.impl;

import com.blgroup.oa.domain.dao.ApplicationDao;
import com.blgroup.oa.domain.entity.ExpenseAccount;
import com.blgroup.oa.domain.entity.SalaryAdjust;
import com.blgroup.oa.domain.entity.Vacation;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ApplicationDaoImpl implements ApplicationDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void saveVacation(Vacation vac) {
        Session session = sessionFactory.openSession();
        session.save(vac);
    }

    @Override
    public List<Vacation> listVacation(String userId) {
        Session session = sessionFactory.openSession();
        String hql = "from Vacation as vac where vac.userId=:userId";
        Query query = session.createQuery(hql);
        query.setParameter("userId", userId);
        return query.list();
    }

    @Override
    public void saveSalaryAdjust(SalaryAdjust salary) {
        Session session = sessionFactory.openSession();
        session.save(salary);
    }

    @Override
    public List<SalaryAdjust> listSalaryAdjust(String userId) {
        String hql = "from SalaryAdjust sa where sa.userId = :userId";
        Session session = sessionFactory.openSession();
        Query query = session.createQuery(hql);
        query.setParameter("userId", userId);
        return query.list();
    }

    @Override
    public void saveExpenseAccount(ExpenseAccount account) {
        Session session = sessionFactory.openSession();
        session.save(account);
    }

    @Override
    public List<ExpenseAccount> listExpenseAccount(String userId) {
        String hql = "from ExpenseAccount ea where ea.userId = :userId";
        Session session = sessionFactory.openSession();
        Query query = session.createQuery(hql);
        query.setParameter("userId", userId);
        return query.list();
    }

}
